package AgentTravel.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JScrollPane;
import java.awt.GridBagConstraints;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import AgentTravel.LogicObjects.City;
import AgentTravel.LogicObjects.CityVisit;
import AgentTravel.LogicObjects.TripData;
import AgentTravel.Messages.PlanMyTripMessage;

import com.toedter.calendar.JDateChooser;
import org.jdesktop.beansbinding.AutoBinding;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;

import java.awt.Insets;
import java.awt.Dimension;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;
import javax.swing.JTextField;
import org.jdesktop.swingbinding.JListBinding;
import org.jdesktop.beansbinding.ObjectProperty;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Component;
import javax.swing.border.CompoundBorder;

public class PlanMyTripDialog extends JDialog {
	private AutoBinding<TripData, Date, JDateChooser, Date> startDateBinding;

	private final JPanel contentPanel = new JPanel();
	private PlanMyTripMessage tripData;
	private JDateChooser startDateChooser;
	private JList choosenCitiesList;
	private List<String> m_cities;
	private JComboBox citiesCombo;
	private JTextField daysToStayField;
	private JTextField daysToStayChoosen;
	/**
	 * Create the dialog.
	 */
	public PlanMyTripDialog(PlanMyTripMessage trip, Collection<String> cities) 
	{
		m_cities = new ArrayList<String>(cities);
		tripData = trip;
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setModal(true);
		setBounds(100, 100, 741, 490);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{0, 0};
		gbl_contentPanel.rowHeights = new int[]{0, 0};
		gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
		{
			JScrollPane scrollPane = new JScrollPane();
			GridBagConstraints gbc_scrollPane = new GridBagConstraints();
			gbc_scrollPane.fill = GridBagConstraints.BOTH;
			gbc_scrollPane.gridx = 0;
			gbc_scrollPane.gridy = 0;
			contentPanel.add(scrollPane, gbc_scrollPane);
			{
				JPanel panel = new JPanel();
				scrollPane.setViewportView(panel);
				GridBagLayout gbl_panel = new GridBagLayout();
				gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
				gbl_panel.rowHeights = new int[]{0, 0, 0, 0};
				gbl_panel.columnWeights = new double[]{1.0, 1.0, 0.0, Double.MIN_VALUE};
				gbl_panel.rowWeights = new double[]{1.0, 0.0, 0.0, Double.MIN_VALUE};
				panel.setLayout(gbl_panel);
				{
					JPanel panel_1 = new JPanel();
					GridBagConstraints gbc_panel_1 = new GridBagConstraints();
					gbc_panel_1.weightx = 0.2;
					gbc_panel_1.gridheight = 1;
					gbc_panel_1.insets = new Insets(0, 0, 5, 5);
					gbc_panel_1.fill = GridBagConstraints.BOTH;
					gbc_panel_1.gridx = 0;
					gbc_panel_1.gridy = 0;
					panel.add(panel_1, gbc_panel_1);
					GridBagLayout gbl_panel_1 = new GridBagLayout();
					gbl_panel_1.columnWidths = new int[]{0, 0, 0};
					gbl_panel_1.rowHeights = new int[]{25, 25, 0, 0, 0};
					gbl_panel_1.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
					gbl_panel_1.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
					panel_1.setLayout(gbl_panel_1);
					{
						JButton addCityButton = new JButton("AddCity");
						GridBagConstraints gbc_addCityButton = new GridBagConstraints();
						gbc_addCityButton.anchor = GridBagConstraints.WEST;
						gbc_addCityButton.insets = new Insets(0, 0, 5, 5);
						gbc_addCityButton.gridx = 0;
						gbc_addCityButton.gridy = 0;
						panel_1.add(addCityButton, gbc_addCityButton);
						addCityButton.addActionListener(new ActionListener() {
							public void actionPerformed(ActionEvent arg0) {
								try
							    {
									int tmpDays = Integer.parseInt(daysToStayField.getText());
									CityVisit c = new CityVisit(citiesCombo.getSelectedItem().toString(), tmpDays);
									tripData.addCity(c);
									choosenCitiesList.repaint();
							    }
								catch (NumberFormatException nfe)
								{
								}
							}
						});
						{
							JButton btnRemovecity = new JButton("RemoveCity");
							btnRemovecity.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent arg0) {
									tripData.removeCity(choosenCitiesList.getSelectedIndex());
									choosenCitiesList.repaint();
								}
							});
							GridBagConstraints gbc_btnRemovecity = new GridBagConstraints();
							gbc_btnRemovecity.anchor = GridBagConstraints.WEST;
							gbc_btnRemovecity.insets = new Insets(0, 0, 5, 5);
							gbc_btnRemovecity.gridx = 0;
							gbc_btnRemovecity.gridy = 1;
							panel_1.add(btnRemovecity, gbc_btnRemovecity);
						}
					}
					{
						citiesCombo = new JComboBox();
						citiesCombo.setAlignmentY(Component.TOP_ALIGNMENT);
						citiesCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
						GridBagConstraints gbc_citiesCombo = new GridBagConstraints();
						gbc_citiesCombo.insets = new Insets(0, 0, 5, 5);
						gbc_citiesCombo.anchor = GridBagConstraints.NORTHWEST;
						gbc_citiesCombo.gridx = 0;
						gbc_citiesCombo.gridy = 2;
						panel_1.add(citiesCombo, gbc_citiesCombo);
					}
					{
						JLabel lblDaysToStay = new JLabel("Days to stay");
						GridBagConstraints gbc_lblDaysToStay = new GridBagConstraints();
						gbc_lblDaysToStay.anchor = GridBagConstraints.WEST;
						gbc_lblDaysToStay.insets = new Insets(0, 0, 0, 5);
						gbc_lblDaysToStay.gridx = 0;
						gbc_lblDaysToStay.gridy = 3;
						panel_1.add(lblDaysToStay, gbc_lblDaysToStay);
					}
					daysToStayField = new JTextField();
					GridBagConstraints gbc_daysToStayField = new GridBagConstraints();
					gbc_daysToStayField.gridx = 1;
					gbc_daysToStayField.gridy = 3;
					panel_1.add(daysToStayField, gbc_daysToStayField);
					daysToStayField.setColumns(10);
				}
				{
					startDateChooser = new JDateChooser();
					startDateChooser.setBorder(new CompoundBorder());
					GridBagConstraints gbc_startDateChooser = new GridBagConstraints();
					gbc_startDateChooser.weightx = 0.4;
					gbc_startDateChooser.anchor = GridBagConstraints.NORTH;
					gbc_startDateChooser.insets = new Insets(0, 0, 5, 5);
					gbc_startDateChooser.gridx = 1;
					gbc_startDateChooser.gridy = 0;
					panel.add(startDateChooser, gbc_startDateChooser);
					startDateChooser.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
					startDateChooser.add(startDateChooser.getCalendarButton());
				}
				{
					choosenCitiesList = new JList();
					choosenCitiesList.setPreferredSize(new Dimension(300, 50));
					choosenCitiesList.setMinimumSize(new Dimension(100, 50));
					GridBagConstraints gbc_choosenCitiesList = new GridBagConstraints();
					gbc_choosenCitiesList.insets = new Insets(0, 0, 5, 0);
					gbc_choosenCitiesList.weightx = 0.6;
					gbc_choosenCitiesList.gridheight = 2;
					gbc_choosenCitiesList.fill = GridBagConstraints.BOTH;
					gbc_choosenCitiesList.gridx = 2;
					gbc_choosenCitiesList.gridy = 0;
					panel.add(choosenCitiesList, gbc_choosenCitiesList);
				}
				{
				}
				{
				}
				{
					JLabel lblDaystostayChoosenOne = new JLabel("DaysToStay choosen one");
					GridBagConstraints gbc_lblDaystostayChoosenOne = new GridBagConstraints();
					gbc_lblDaystostayChoosenOne.anchor = GridBagConstraints.WEST;
					gbc_lblDaystostayChoosenOne.insets = new Insets(0, 0, 5, 5);
					gbc_lblDaystostayChoosenOne.gridx = 0;
					gbc_lblDaystostayChoosenOne.gridy = 1;
					panel.add(lblDaystostayChoosenOne, gbc_lblDaystostayChoosenOne);
				}
				daysToStayChoosen = new JTextField();
				daysToStayChoosen.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent arg0) {
						choosenCitiesList.repaint();
					}
				});
				daysToStayChoosen.addFocusListener(new FocusAdapter() {
					@Override
					public void focusLost(FocusEvent e) {
							      JTextField textField = (JTextField)e.getSource();
							      String content = textField.getText();
							      if (content.length() != 0) {
							        try {
							          Integer.parseInt(content);
							        } catch (NumberFormatException nfe) {
							          getToolkit().beep();
							          JOptionPane.showMessageDialog(textField,
							        		  "Type a valid number!", "Wrong number!", JOptionPane.ERROR_MESSAGE);
							          textField.setText("1");
							          textField.requestFocus();
							        }
							      }
					}
				});
				GridBagConstraints gbc_daysToStayChoosen = new GridBagConstraints();
				gbc_daysToStayChoosen.insets = new Insets(0, 0, 5, 5);
				gbc_daysToStayChoosen.fill = GridBagConstraints.HORIZONTAL;
				gbc_daysToStayChoosen.gridx = 1;
				gbc_daysToStayChoosen.gridy = 1;
				panel.add(daysToStayChoosen, gbc_daysToStayChoosen);
				daysToStayChoosen.setColumns(10);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						setVisible(false);
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						tripData = null;
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		initDataBindings();
	}
	protected void initDataBindings() {
		BeanProperty<PlanMyTripMessage, List<City>> planMyTripMessageBeanProperty_1 = BeanProperty.create("tripPlan");
		JListBinding<City, PlanMyTripMessage, JList> jListBinding = SwingBindings.createJListBinding(UpdateStrategy.READ, tripData, planMyTripMessageBeanProperty_1, choosenCitiesList);
		jListBinding.bind();
		//
		JComboBoxBinding<String, List<String>, JComboBox> jComboBinding = SwingBindings.createJComboBoxBinding(UpdateStrategy.READ, m_cities, citiesCombo, "citiesComboBinding");
		jComboBinding.bind();
		//
		BeanProperty<PlanMyTripMessage, Date> planMyTripMessageBeanProperty = BeanProperty.create("tripStartTime.time");
		BeanProperty<JDateChooser, Date> jDateChooserBeanProperty = BeanProperty.create("date");
		AutoBinding<PlanMyTripMessage, Date, JDateChooser, Date> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, tripData, planMyTripMessageBeanProperty, startDateChooser, jDateChooserBeanProperty);
		autoBinding.bind();
		//
		BeanProperty<JList, Integer> jListBeanProperty = BeanProperty.create("selectedElement.daysToStay");
		BeanProperty<JTextField, String> jTextFieldBeanProperty = BeanProperty.create("text");
		AutoBinding<JList, Integer, JTextField, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ_WRITE, choosenCitiesList, jListBeanProperty, daysToStayChoosen, jTextFieldBeanProperty);
		autoBinding_1.bind();
	}
	
	public PlanMyTripMessage getMessage()
	{
		setVisible(true);
		return tripData;
	}
}
